<html xmlns:th="http://www.thymeleaf.org"
      xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
      >
<head th:replace="fragments/tabhead::headc"></head>

<body>

<th:block layout:fragment="content">
<!-- 销售订单 -->
<div style="margin-top:15px">
  <form class="search layui-form" id="searchForm">
  <div class="layui-form-item layui-elem-quote" style="padding:10px;">
     <div class="layui-inline">
      <label class="layui-form-label" style="width:90px">所属公司</label>
      <div class="layui-input-inline">
        <select name="companyId"><option value="1" th:each="c:${companys}" th:text="${c.value}" th:value="${c.code}"></option></select>
      </div>
    </div>
    <div class="layui-inline">
      <label class="layui-form-label" style="width:80px">客户</label>
      <div class="layui-input-inline" style="width:100px">
        <input type="tel" name="customerName" class="layui-input"/>
      </div>
    </div>
   <div class="layui-inline">
      <label class="layui-form-label" style="width:100px">审核状态</label>
      <div class="layui-input-inline" style="width:100px;">
        <select name="reviewStatus">
        	<option value="">全部</option>
        	<option th:each="s : ${status}" th:attr="value=${s.code}" th:text="${s.value}"></option>
        </select>
      </div>
    </div>
    <div class="layui-inline">
      <label class="layui-form-label" style="width:60px">时间</label>
      <div class="layui-input-inline" style="width:100px">
        <input type="text" id="startDate" class="layui-input" name='startDate'/>
      </div>
       <div class="layui-form-mid">-</div>
      <div class="layui-input-inline" style="width:100px">
        <input type="text" id="endDate" class="layui-input" name="endDate"/>
      </div>
    </div>
	
    <div class="layui-inline">
      <div class="layui-input-inline" style="width:60px;">
        <button class="layui-btn" id="searchBtn" type="button">查询</button>
      </div>
      <div class="layui-input-inline" style="width:60px;">
        <button class="layui-btn" id="addBtn" type="button" authcode='800005'>添加</button>
      </div>
    </div>
    
  </div>
 </form>
</div>

<table class="layui-table"  id="tab" lay-filter="tab">
</table>

<script th:inline="javascript">

var ins,page,trobj,editdialogindex,adddialogindex;
$(function(){ 
	
	layui.use(['table','layer','laypage'], function(){
		var table = layui.table;
		var laypage = layui.laypage;
		ins =table.render({ //其它参数在此省略
		 elem:"#tab",
		 url: '/sale/saleorder/list?first=true',
		 cols:[[
			   //fixed 列将会被固定在左或右，不随滚动条而滚动left right
			  {field : 'billNo',sort: true,width:140,title : '销售单号',fixed:"left"},
			  {field : 'companyName',width:180,title : '所属公司'},
			  {field : 'sourceBillNo',width:120,title : '来源单据编号'},
			  {field : 'customerName',width:180,title : '客户名称'},
			  {field : 'customerContactName',width:110,title : '客户联系人'},
			  {field : 'customerContactPhone',width:120,title : '联系电话'},
			  {field : 'customerAadr',width:180,title : '收货地址'},
			  {field : 'statementName',width:125,title : '结算方式'},
			  {field : 'statementDays',width:125,title : '结算天数'},
			  {field : 'statementCurrencyName',width:125,title : '结算币种'},
			  {field : 'statementExchangeRateName',width:125,title : '汇率'},
			  {field : 'statementTaxRateName',width:125,title : '税率'},
			  {field : 'orderAccount',width:140,title : '订单总量'},
			  {field : 'deliveredQuantity',width:140,title : '已交付数量'},
			  {field : 'undeliveredQuantity',width:140,title : '未交付数量'},
			  {field : 'origDiscountAmount',width:140,title : '订单未税总金额'},
			  {field : 'origTaxAmount',width:140,title : '订单含税总金额'},
			  {field : 'reviewStatusName',width:80,title : '审核状态'},
			  {field : 'reviewName',width:110,title : '审核人'},
			  {field : 'reviewTime',width:110,title : '审核时间'},
			  {field : 'modifyName',width:110,title : '修改人'},
			  {field : 'modifyTime',width:110,title : '修改时间'},
			  {field : 'creatorName',width:100,title : '创建人'},
			  {field : 'ctime',width:110,title : '创建时间'},
			  {field : 'remark',width:300,title : '备注'},
		      {align:'center',fix:'right',width:250, toolbar:'#toolbar',title:'操作',fixed:"right"}
		 ]]
	    ,page: true //是否显示分页
	    ,limits: [10,20,50]
	    ,limit:10 //每页默认显示的数量
		  //where:{phone:phone}
		 ,method: 'post'
		 ,done:function(res,curr,count){
			// console.log(res);
			 page = curr;
			/*     //得到当前页码
			 console.log(curr); 
			    //得到数据总量
			 console.log(count); */
		 }
		});
		
		table.on("tool(tab)",function(obj){
			var data = obj.data; //获得当前行数据
			var layEvent = obj.event; //获得 lay-event 对应的值
			trobj = obj;
			var tr = obj.tr; //获得当前行 tr 的DOM对象
			if(layEvent=='edit'){
				$.post('/sale/saleorder/'+data.companyId+'/'+data.id, {}, function(str){
				editdialogindex = layer.open({
									title:"修改销售订单",
								    type: 1,
								    area: ['680px', '590px'],
								    content: str,//注意，如果str是object，那么需要字符拼接。
								    zIndex:10
								  });
								});
			
			//删除
			}else if(layEvent=='del'){
				var confirmindex = layer.confirm('您确定删除该记录吗？', {
					  btn: ['确定','取消'] //按钮
					}, function(){
						$.post("/sale/saleorder/remove?id="+data.id,function(data){
							if(data.code==200){
								trobj.del();
								layer.close(confirmindex)
							}else{
								layer.msg(data.message);
							}
						});
					}, function(){
					  	
					});
			//关闭
			}else if(layEvent=="close"){
				var confirmindex = layer.confirm('您确定关闭该记录吗？', {
					  btn: ['确定','取消'] //按钮
					}, function(){
						$.post("/sale/saleorder/close?id="+data.id,function(data){
							if(data.code==200){
								//更新表格数据
								//trobj.update(data.result);
								reloadData();
								$(tr).find(":last")
								layer.close(confirmindex)
							}else{
								layer.msg(data.message);
							}
						});
					}, function(){
					  	
					});
			//审核
			}else if(layEvent=="review"){
				var confirmindex = layer.confirm('您确定审核该记录吗？', {
					  btn: ['确定','取消'] //按钮
					}, function(){
						$.post("/sale/saleorder/review?id="+data.id,function(data){
							if(data.code==200){
								reloadData();
								layer.close(confirmindex)
							}else{
								layer.msg(data.message);
							}
						});
					}, function(){
					  	
					});
			//取消审核
			}else if(layEvent=="cancelReview"){
				//询问框
				var confirmindex = layer.confirm('您确定弃审改该录吗？', {
												  btn: ['确定','取消'] //按钮
												}, function(){
													$.post("/sale/saleorder/cancelreview?id="+data.id,function(data){
														if(data.code==200){
															//更新数据
															reloadData();
															layer.close(confirmindex)
														}else{
															layer.msg(data.message);
														}
													});
												}, function(){
												  	
												});
				
			}else if(layEvent=="detail"){
				menuOpen("/sale/saleorder/detail?id="+data.id,"销售订单【"+data.billNo+"】明细")
				//location.href="/sale/saleorder/detail?id="+data.id;
			}
		});
		
		
		//查询按钮点击
		$("#searchBtn").click(function(){
			var table = layui.table;
		    ins.reload({url: '/sale/saleorder/list',where:$("#searchForm").serializeJson()})
		});
		//增加按钮点击
		$("#addBtn").click(function(){
			$.post('/sale/saleorder/addinit', {companyId:$("select[name='companyId'] option:selected").val()}, function(str){
				adddialogindex = layer.open({
									id:"addSaleOrder",
									title:"新增销售订单",
								    type: 1,
								    area: ['680px', '590px'],
								    content: str,//注意，如果str是object，那么需要字符拼接。
								    zIndex:10
								  });
								});
		})
		
	})
	
	layui.use("laydate",function(){
		var laydate = layui.laydate;
		laydate.render({
			  elem: '#startDate' //指定元素
			});
		laydate.render({
			  elem: '#endDate' //指定元素
			});
	});
}); 
function reloadData(){
	$(".layui-laypage-btn")[0].click();
	
}
</script>
<script type="text/html" id="toolbar">
	<a class="layui-btn layui-btn-mini" lay-event="detail" authcode="800011">明细</a>
	{{#if (d.reviewStatus==0){}}
		<a class="layui-btn layui-btn-mini" lay-event="review" authcode="800008">审核</a>
		<a class="layui-btn layui-btn-danger layui-btn-mini" lay-event="del" authcode="800012">删除</a>
		<a class="layui-btn layui-btn-danger layui-btn-mini" lay-event="close" authcode="800010">关闭</a>
		<a class="layui-btn layui-btn-mini" lay-event="edit" authcode="800006">编辑</a>
	{{# }else if(d.reviewStatus==1){}}
		<a class="layui-btn layui-btn-mini" lay-event="cancelReview">弃审</a>
	{{# }}}
</script>
<script type="text/html" id="taxTpl">
<span>{{d.taxRate}}%</span>
</script>
<!--<div th:replace="department/model::modelDiv"></div>-->
</th:block>
</body>
</html>